﻿using System;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace COMTester
{
    public partial class Form1 : Form
    {
        SynchronizationContext SyncContext = null;
        public Form1()
        {
            InitializeComponent();
            SyncContext = SynchronizationContext.Current;
            foreach (string com in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(com);
            }
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox1.SelectedIndex = 0;
        }

        ~Form1()
        {
            if (serialPort1.IsOpen)
                serialPort1.Close();
        }

        void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (serialPort1.IsOpen)     //判断是否打开串口
            {

                Byte[] receivedData = new Byte[serialPort1.BytesToRead];        //创建接收字节数组
                serialPort1.Read(receivedData, 0, receivedData.Length);         //读取数据
                string data = Encoding.ASCII.GetString(receivedData);
                //触发整条记录的处理   
                if (data.Length > 0)
                {
                    SyncContext.Post(recvMsg, data);
                }

            }
        }

        public void recvMsg(object _msg)
        {
            DateTime dt = DateTime.Now;
            ListViewItem item = new ListViewItem(dt.ToString());
            item.SubItems.Add(_msg.ToString());
            listView1.Items.Add(item);

            if(_msg.ToString()=="hello")
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Write("hello");
                }
            }
        }
        /// <summary>
        /// 错误处理函数
        /// </summary>
        void comPort_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Close();
                button1.Text = "打开";
                return;
            }            

            string comname = comboBox1.Text;
            if (serialPort1.IsOpen)
            { }
                serialPort1.Close();
            if (comname.Length != 0)
            {
                serialPort1.PortName = comname;
                serialPort1.BaudRate = 115200;
                serialPort1.Open();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
                serialPort1.ErrorReceived += new SerialErrorReceivedEventHandler(comPort_ErrorReceived);
            }
            if (serialPort1.IsOpen)
                button1.Text = "关闭";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                serialPort1.Write(richTextBox1.Text);
            }
        }
    }
}
